//
//  MYShopGoodsCell.swift
//  MuYing
//
//  Created by tbb on 2019/8/22.
//  Copyright © 2019 TBB. All rights reserved.
//

import UIKit

@objc protocol MYShopGoodsCellDelegate:NSObjectProtocol {
    
    func didSelectItemAtImageAction(_ indexPath:NSIndexPath,_ item:NSInteger);
    
}

class MYShopGoodsCell: YQTableViewBaseCell,UICollectionViewDelegate, UICollectionViewDataSource {
//    @objc var catalogModel:YQPShopCatalog2Model? {
//        didSet {
//            if let arr = self.catalogModel?.catalog3List {
//                self.goodsArray = arr as? [YQPShopCatalog3Model];
//            }
//            //self.goodsArray = self.catalogModel?.catalog3List as? [YQPShopCatalog3Model];
//        }
//    }
    
    @objc var goodsArray:[YQPShopCatalog3Model]? {
        didSet {
            self.goodsCollView.reloadData();
        }
    }
    
    @objc weak var delegate: MYShopGoodsCellDelegate?
    @objc var selectIndexPath:NSIndexPath?;
    static var categoryCollViewItemSizeW = (88 / 375.0) * kSwiftkScreenSizeWidth;
    static var goodsCellW = ((kSwiftkScreenSizeWidth - categoryCollViewItemSizeW) - (24 + 20))/3.0;
    static var goodsCellH = (95 / 81.0) * goodsCellW;

    lazy var bgView: UIView = {
        let bgView = UIView();
        bgView.backgroundColor = UIColor.white;
        bgView.layer.cornerRadius = 8;
        bgView.clipsToBounds = true;
        return bgView;
    }()

    
    lazy var goodsCollView: UICollectionView = {
        let layout = UICollectionViewFlowLayout();
        
        layout.itemSize = CGSize(width: MYShopGoodsCell.goodsCellW, height: MYShopGoodsCell.goodsCellH);
        layout.minimumLineSpacing = 10.0;
        layout.minimumInteritemSpacing = 8.0;
        let cityCollView = UICollectionView(frame: CGRect(x: 12, y: 0, width: (kSwiftkScreenSizeWidth - MYShopGoodsCell.categoryCollViewItemSizeW) - (24 + 20), height: 50), collectionViewLayout: layout);
        cityCollView.backgroundColor = UIColor.white;//UIColorFromHex(rgbValue: 0xF6F6F6);
        cityCollView.delegate = self;
        cityCollView.dataSource = self;
        cityCollView.bounces = false;
        cityCollView.showsVerticalScrollIndicator = false;
        
        cityCollView.register(MYShopGoodsCollViewCell.self, forCellWithReuseIdentifier: "MYShopGoodsCollViewCell")
        return cityCollView;
    }()
    
    
    override func layoutSubviews() {
        super.layoutSubviews();
        self.contentView.backgroundColor = UIColorFromHex(rgbValue: 0xF6F6F6);
        self.backgroundColor = UIColorFromHex(rgbValue: 0xF6F6F6);
        self.contentView.addSubview(self.bgView);
        self.bgView.mas_makeConstraints { (make) in
            make?.right.mas_equalTo()(-12);
            make?.left.mas_equalTo()(12);
            make?.top.mas_equalTo()(0);
            make?.bottom.mas_equalTo()(0);
        }
        
        
        self.bgView.addSubview(self.goodsCollView);
        self.goodsCollView.mas_makeConstraints { (make) in
            make?.top.mas_equalTo()(10);
            make?.bottom.mas_equalTo()(-10);
            make?.left.mas_equalTo()(0);
            make?.right.mas_equalTo()(0);
        }
        
    }
    
//    @objc func setModel(_ model:YQPCatalogModel)
//    {
//        self.catalogModel = model
//        self.goodsArray.removeAllObjects()
//        self.goodsArray.addObjects(from: model.catalog3List)
//        self.goodsCollView.reloadData()
//    }

}

extension MYShopGoodsCell {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return self.goodsArray?.count ?? 0;
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) ->
        UICollectionViewCell {
            guard let cArr = self.goodsArray else {
                return UICollectionViewCell();
            }

            let model:YQPShopCatalog3Model = cArr[indexPath.item];
            
            guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "MYShopGoodsCollViewCell", for: indexPath) as? MYShopGoodsCollViewCell else {
            return UICollectionViewCell();
        }
            cell.titleLb.text = model.name;
            
            //cell.goodsIcon.kf.setImage(with: URL(string: model.image))
            cell.goodsIcon.setImageWith(URL(string: model.image), placeholder: nil)
        return cell;
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        

        if let delegate = self.delegate, delegate.responds(to: Selector(("didSelectItemAtImageAction::"))), let selectIndexPath = self.selectIndexPath {
            self.delegate?.didSelectItemAtImageAction(selectIndexPath,indexPath.item);
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, didDeselectItemAt indexPath: IndexPath) {

    }
}
